package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Category;
import com.itheima.pojo.Employee;
import com.itheima.service.CategoryService;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;


    /**
     * 登录后台
     *
     * @param employee
     * @param httpSession
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Employee employee, HttpSession httpSession) {
        //获取用户名
        String empUsername = employee.getUsername();
        //查询表中的用户名
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", empUsername);
        Employee emp = employeeService.getOne(queryWrapper);
        //判断
        if (emp == null) {
            //不存在
            return R.error("用户名不存在");
        }
        //用户名存在。比较密码
        //获取密码
        String empPassword = employee.getPassword();
        //加密
        String empMd5Password = DigestUtils.md5DigestAsHex(empPassword.getBytes(StandardCharsets.UTF_8));
        //先获取数据库的密码
        String password = emp.getPassword();
        //判断密码
        if (!empMd5Password.equals(password)) {
            //不相等
            return R.error("密码错误");
        }
        //判断状态是否可用
        if (emp.getStatus() == 0) {
            //禁用
            return R.error("该账户被禁用");
        }
        //登录成功-将数据存储到session域中，在一次会话中共享数据
        httpSession.setAttribute("employee", emp.getId());
        //响应给用户成功
        return R.success(emp);
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public R logout(HttpSession session){
        //清楚session域中的登录信息
        session.removeAttribute("employee");
        //响应退出信息给前端
        return R.success("退出成功");
    }

    /**
     * 新增员工
     * @param employee
     * @param session
     * @return
     */
    @PostMapping
    public R save(@RequestBody Employee employee,HttpSession session){
        //补全employee的信息
        //对初始密码进行加密
        employee.setPassword(DigestUtils.md5DigestAsHex("12345".getBytes(StandardCharsets.UTF_8)));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        Long userId = (Long) session.getAttribute("employee");
//        employee.setCreateUser(userId);
//        employee.setUpdateUser(userId);
        //调用存储数据的方法
        employeeService.save(employee);
        return R.success("添加成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R page(Integer page,Integer pageSize,String name){
        //获取请求参数
        IPage<Employee> pageInfo = new Page<>(page,pageSize);
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),"name",name);
        //降序排列
        queryWrapper.orderByDesc("create_time");
        employeeService.page(pageInfo,queryWrapper);
        //响应数据
        return R.success(pageInfo);
    }

    /**
     * 修改状态
     * @param employee
     * @return
     */
    @PutMapping
    public R updateStatus(@RequestBody Employee employee){
//        //设置修改时间
//        employee.setUpdateTime(LocalDateTime.now());
//        //从session域中获取当前登录用户
//        long userId = (long) session.getAttribute("employee");
//        //设置修改人
//        employee.setUpdateUser(userId);
        //调用方法 根据id修改状态
        employeeService.updateById(employee);
        //给前端响应数据
        return R.success("修改成功");
    }

    /**
     * 回显数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R selectById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }


}

